package utils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.StringTokenizer;

public class DateUtils {

	
	/**************************************
	 * Method name		: LayNgayThangHienTai
	 * Return type		: String
	 * Decription		:Lay ra ngay thang nam hien tai voi format :ngay/thang/nam.
	 * Created date		: Jun 14, 2008
	 * Author			: Honglk
	 **************************************/
	public static String LayNgayThangHienTai() {
		Date today = new Date();
		SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
		String datenewformat = formatter.format(today);
		return datenewformat;
	}
	
	/**************************************
	 * Method name		: formatDateUtilToString
	 * Return type		: String
	 * Decription		:Dinh dang lai kieu ngay de hien thi
	 * Created date		: Jul 17, 2008
	 * Author			: Honglk
	 **************************************/
	public static String formatDateUtilToString(Date date){
		SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
		String datenewformat = formatter.format(date);
		return datenewformat;
	}
	
	/**************************************
	 * Method name		: LayNgayThangHienTai1
	 * Return type		: String
	 * Decription		:Lay ra ngay thang nam hien tai voi format :nam-thang-ngay.
	 * Created date		: Jun 14, 2008
	 * Author			: Honglk
	 **************************************/
	public static String LayNgayThangHienTai1() {
		Date today = new Date();
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
		String datenewformat = formatter.format(today);
		return datenewformat;
	}
	public static String LayNgayThangHienTaiGio1() {
		Date today = new Date();
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String datenewformat = formatter.format(today);
		return datenewformat;
	}

	
	/**
	 * Ham lay ngay thang, gio hien tai cua He thong,voi format :ngay/thang/nam,gio:phut.
	 * 
	 * @return String
	 */
	public static String LayNgayThangGioHienTai() {
		Date today = new Date();
		SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy HH:mm");
		String datenewformat = formatter.format(today);
		return datenewformat;
	}

	/**
	 * <p>
	 * Description: Ham doi chuoi kieu String thanh
	 *  kieu Date voi input co format:ngay/thang/nam hoac nam-thang-ngay
	 * </p>
	 * <p>
	 * Input: String
	 * </p>
	 * <p>
	 * Output: DateCalendar
	 * </p>
	 * 
	 */
	public static Calendar DoiChuoiThanhNgayThang(String strDate) {
		int ngay = Integer.parseInt(TrichNgay(strDate));
		int thang = Integer.parseInt(TrichThang(strDate));
		int nam = Integer.parseInt(TrichNam(strDate));
		Calendar c = Calendar.getInstance();
		c.set(nam, thang,ngay);
		return c;
	}
	/**
	 * <p>
	 * Description: Ham doi chuoi kieu String thanh
	 *  kieu Date voi input co format:ngay/thang/nam hoac nam-thang-ngay
	 * </p>
	 * <p>
	 * Input: String
	 * </p>
	 * <p>
	 * Output: DateCalendar
	 * </p>
	 * 
	 */
	public static Calendar DoiChuoiThanhNgayThangGioPhut(String strDate) {
//		StringTokenizer token = new StringTokenizer(strDate,"");
		int ngay = Integer.parseInt(TrichNgay(strDate));
		int thang = Integer.parseInt(TrichThang(strDate));
		int nam = Integer.parseInt(TrichNam(strDate));
		int gio = Integer.parseInt(TrichGio(strDate));
		int phut = Integer.parseInt(TrichPhut(strDate));
		Calendar c = Calendar.getInstance();
		c.set(nam, thang,ngay,gio,phut);
		return c;
	}
	
	/**************************************
	 * Method name		: formatDate
	 * Return type		: String
	 * Decription		:Dinh dang lai kieu ngay: ngay/thang/nam,voi kieu ngay
	 * 					 nhap vao la:nam-thang-ngay cho phu hop voi csdl la postgress
	 * Created date		: Jun 14, 2008
	 * Author			: Honglk
	 **************************************/
	public static String formatDate(String date) {
		String res = "";
		String year = "";
		String month = "";
		String day = "";
		try {
			StringTokenizer token = new StringTokenizer(date, "-");
			while (token.hasMoreElements()) {
				year = token.nextToken();
				month = token.nextToken();
				day = token.nextToken();
			}
			res = day + "/" + month + "/" + year;
		} catch (Exception e) {
			 res = "";
		}
		return res;
	}
	/**************************************
	 * Method name		: formatDate1
	 * Return type		: String
	 * Decription		:Dinh dang lai kieu ngay cho phu hop voi postgrest
	 * 					voi input la:ngay/thang/nam ->nam-thang-ngay
	 * Created date		: Jun 16, 2008
	 * Author			: Honglk
	 **************************************/
	public static String formatDate1(String date) {
		String res = "";
		String year = "";
		String month = "";
		String day = "";
		try {
			StringTokenizer token = new StringTokenizer(date, "/");
			while (token.hasMoreElements()) {
				day = token.nextToken();
				month = token.nextToken();
				year = token.nextToken();
			}
			res = year + "-" + month + "-" + day;
		} catch (Exception e) {
			
		}
		return res;
	}
	
	/**************************************
	 * Method name		: utilDateToSqlDate
	 * Return type		: java.sql.Date
	 * Decription		:Chuyen kieu date cua lop util thanh kieu date cua sql
	 * Created date		: Jun 17, 2008
	 * Author			: Honglk
	 **************************************/
	public static java.sql.Date utilDateToSqlDate(java.util.Date uDate){
		try{
		DateFormat sqlDateFormatter = new SimpleDateFormat("yyyy-MM-dd");
		return java.sql.Date.valueOf(sqlDateFormatter.format(uDate));
		}catch(Exception e){	
		}
		return  null;
	}
	
	/**************************************
	 * Method name		: sqlDateToutilDate
	 * Return type		: java.util.Date
	 * Decription		:Chuyen kieu date cua SQL thanh kieu date cua util
	 * Created date		: Jun 17, 2008
	 * Author			: Honglk
	 **************************************/
	public static java.util.Date sqlDateToutilDate(java.sql.Date sDate) {
		try {
			DateFormat utilDateFormatter = new SimpleDateFormat("dd-MM-yyyy");
			return (java.util.Date) utilDateFormatter.parse(utilDateFormatter
					.format(sDate));
		} catch (ParseException e) {
		}
		return new Date();
	}



	/**************************************
	 * Method name		: checkIsInteger
	 * Return type		: boolean
	 * Decription		:Kiem tra xem 1 chuoi co phai la so hay khong?
	 * Created date		: Jun 14, 2008
	 * Author			: Honglk
	 **************************************/
	
	public static boolean checkIsInteger(String arg) {
		boolean result = false;
		try {
			Integer.parseInt(arg);
			result = true;
		} catch (Exception e) {
			result = false;
		}
		return result;
	}
	
	
	/**************************************
	 * Method name		: checkIsDate
	 * Return type		: boolean
	 * Decription		:Kiem tra xem 1 chuoi nhap vao co phai le kieu date
	 * Created date		: Jun 14, 2008
	 * Author			: Honglk
	 **************************************/
	@SuppressWarnings("deprecation")
	public static boolean checkIsDate(String arg) {
		boolean result = false;
		try {
			Date.parse(formatDate(arg));
			result = true;
		} catch (Exception e) {
			result = false;
		}
		return result;
	}

	
	
	/**
	 * <p>
	 * Description: Ham lay thu (Ngay trong tuan) cua ngay hien tai
	 * </p>
	 * <p>
	 * Input:
	 * </p>
	 * <p>
	 * Output: Integer
	 * </p>
	 * 
	 */
	public static int LayThuHienTai() {
		Calendar rightNow = Calendar.getInstance();
		return (rightNow.get(Calendar.DAY_OF_WEEK));
	}

	/**
	 * <p>
	 * Description: Ham lay ngay trong thang (01..31) cua ngay hien tai
	 * </p>
	 * <p>
	 * Input:
	 * </p>
	 * <p>
	 * Output: Integer
	 * </p>
	 * 
	 */
	public static int LayNgayHienTai() {
		Calendar rightNow = Calendar.getInstance();
		return (rightNow.get(Calendar.DAY_OF_MONTH));
	}

	/**
	 * <p>
	 * Description: Ham lay thang hien tai
	 * </p>
	 * <p>
	 * Input:
	 * </p>
	 * <p>
	 * Output: Integer
	 * </p>
	 * 
	 */
	public static int LayThangHienTai() {
		Calendar rightNow = Calendar.getInstance();
		return (rightNow.get(Calendar.MONTH) + 1);
	}

	/**
	 * <p>
	 * Description: Ham lay Quy cua Nam (1..4) cua ngay hien tai
	 * </p>
	 * <p>
	 * Input:
	 * </p>
	 * <p>
	 * Output: Integer
	 * </p>
	 * 
	 */
	public static int LayQuyHienTai() {
		Calendar rightNow = Calendar.getInstance();

		int Thang = rightNow.get(Calendar.MONTH) + 1;
		int Quy = 0;

		switch (Thang) {
		case 1:
			Quy = 1;
			break;
		case 2:
			Quy = 1;
			break;
		case 3:
			Quy = 1;
			break;

		case 4:
			Quy = 2;
			break;
		case 5:
			Quy = 2;
			break;
		case 6:
			Quy = 2;
			break;

		case 7:
			Quy = 3;
			break;
		case 8:
			Quy = 3;
			break;
		case 9:
			Quy = 3;
			break;

		case 10:
			Quy = 4;
			break;
		case 11:
			Quy = 4;
			break;
		case 12:
			Quy = 4;
			break;

		}
		return (Quy);
	}

	/**
	 * <p>
	 * Description: Ham lay nam hien tai
	 * </p>
	 * <p>
	 * Input:
	 * </p>
	 * <p>
	 * Output: Integer
	 * </p>
	 * 
	 */
	public static int LayNamHienTai() {
		Calendar rightNow = Calendar.getInstance();
		return (rightNow.get(Calendar.YEAR));
	}
	
	/**************************************
	 * Method name		: ngayBatDauCuaQuy
	 * Return type		: String
	 * Decription		:lay ra ngay,thang bat dau cua quy.
	 * Created date		: Jul 13, 2008
	 * Author			: Honglk
	 **************************************/
	public static String ngayBatDauCuaQuy(String quy){
		String starDate = "";
		int quyInt = Integer.parseInt(quy);
		switch (quyInt) {
		case 13: //Quy 1
			starDate = "1-1";
			break;
		case 14: //Quy 2
			starDate = "4-1";
			break;
		case 15: //Quy 3
			starDate = "7-1";
			break;
		case 16: //Quy 4
			starDate = "10-1";
			break;

		default:
			break;
		}
		return starDate;
	}
	/**************************************
	 * Method name		: ngayBatDauCuaQuy
	 * Return type		: String
	 * Decription		:lay ra ngay,thang ket thuc cua quy.
	 * Created date		: Jul 13, 2008
	 * Author			: Honglk
	 **************************************/
	public static String ngayKetThucCuaQuy(String quy){
		String endDate = "";
		int quyInt = Integer.parseInt(quy);
		switch (quyInt) {
		case 13: //Quy 1
			endDate = "3-31";
			break;
		case 14: //Quy 2
			endDate = "6-31";
			break;
		case 15: //Quy 3
			endDate = "9-31";
			break;
		case 16: //Quy 4
			endDate = "12-31";
			break;
			
		default:
			break;
		}
		return endDate;
	}
	
	/**************************************
	 * Method name		: ngayBatDauTrongTuan
	 * Return type		: String
	 * Description		:lay ngay bat dau cua tuan
	 * Created date		: Jul 29, 2008
	 * Author			: Honglk
	 **************************************/
	public static String ngayBatDauTrongTuan(String tuan){
		String dateWeek = "";
		int tuanInt = Integer.parseInt(tuan);
		switch (tuanInt) {
		case 1: //Tuan 1
			dateWeek = "1";
			break;
		case 2: //Tuan 2
			dateWeek = "8";
			break;
		case 3: //Tuan 3
			dateWeek = "16";
			break;
		case 4: //Tuan 4
			dateWeek = "24";
			break;

		default:
			break;
		}
		return dateWeek;
	}
	/**************************************
	 * Method name		: ngayKetThucTrongTuan
	 * Return type		: String
	 * Description		:lay ngay ket thuc cua tuan
	 * Created date		: Jul 29, 2008
	 * Author			: Honglk
	 **************************************/
	public static String ngayKetThucTrongTuan(String tuan){
		String dateWeek = "";
		int tuanInt = Integer.parseInt(tuan);
		switch (tuanInt) {
		case 1: //Tuan 1
			dateWeek = "7";
			break;
		case 2: //Tuan 2
			dateWeek = "15";
			break;
		case 3: //Tuan 3
			dateWeek = "23";
			break;
		case 4: //Tuan 4
			dateWeek = "31 ";
			break;
			
		default:
			break;
		}
		return dateWeek;
	}

	/**
	 * <p>
	 * Description: Ham tinh tong so thang
	 * </p>
	 * <p>
	 * Input: String tuthang, String tunam, String denthang, String dennam
	 * </p>
	 * 
	 * <p>
	 * Output: Integer
	 * </p>
	 * 
	 */
	public static int TinhTongSoThang(String tuthang, String tunam, String denthang,
			String dennam) {
		int KetQua = 0;
		int TuThang = Integer.parseInt(tuthang);
		int TuNam = Integer.parseInt(tunam);
		int DenThang = Integer.parseInt(denthang);
		int DenNam = Integer.parseInt(dennam);

		KetQua = (TuThang + TuNam) - (DenThang + DenNam) + 1;
		return KetQua;
	}
	
	/**
	 * <p>
	 * Description: Ham trich lay gio trong mot kieu date cho truoc
	 * </p>
	 * <p>
	 * Input: String strDate(yyyy-MM-dd-HH-SS hoac dd/MM/yyyy HH:SS)
	 * </p>
	 * 
	 * <p>
	 * Output: String
	 * </p>
	 * 
	 */
	public static String TrichPhut(String strDate) {
		
		//theo dinh dang: yyyy-MM-dd-HH-SS
		 if (strDate.indexOf("-") != -1) {
			String arr[] = new String[5];
			arr = strDate.split("-");

			return (arr[4]);
		}
		//theo dinh dang:dd/MM/yyyy HH:SS
		else 
		{
			StringTokenizer token = new StringTokenizer(strDate,"/,: ");
			token.nextToken();
			token.nextToken();
			token.nextToken();
			token.nextToken();
			return token.nextToken();
		}
	}
	
	/**
	 * <p>
	 * Description: Ham trich lay gio trong mot kieu date cho truoc
	 * </p>
	 * <p>
	 * Input: String strDate(dd/MM/yyyy HH:SS hoac yyyy-MM-dd-HH-SS)
	 * </p>
	 * 
	 * <p>
	 * Output: String
	 * </p>
	 * 
	 */
	public static String TrichGio(String strDate) {
		
		//theo dinh dang: yyyy-MM-dd-HH-SS
		if (strDate.indexOf("-") != -1) {
			String arr[] = new String[4];
			arr = strDate.split("-");

			return (arr[3]);
		}
		//theo dinh dang:dd/MM/yyyy HH:SS
		else 
		{
			StringTokenizer token = new StringTokenizer(strDate,"/,: ");
			token.nextToken();
			token.nextToken();
			token.nextToken();
			return token.nextToken();
		}
	}

	/**
	 * <p>
	 * Description: Ham trich lay ngay trong mot kieu date cho truoc
	 * </p>
	 * <p>
	 * Input: String strDate
	 * </p>
	 * 
	 * <p>
	 * Output: String
	 * </p>
	 * 
	 */
	public static String TrichNgay(String strDate) {
//		//theo dinh dang:dd/MM/yyyy
//		if (strDate.indexOf("/") != -1) {
//			String arr[] = new String[3];
//
//			arr = strDate.split("/");
//
//			return (arr[0]);
//		}
		//theo dinh dang: yyyy-MM-dd
		 if (strDate.indexOf("-") != -1) {
			String arr[] = new String[3];

			arr = strDate.split("-");

			return (arr[2]);
		}
		//theo dinh dang:dd/MM/yyyy
			else 
			{
				StringTokenizer token = new StringTokenizer(strDate,"/,: ");
				return token.nextToken();
		}
	}

	/**
	 * <p>
	 * Description: Ham trich lay thang trong mot kieu date cho truoc
	 * </p>
	 * <p>
	 * Input: String strDate
	 * </p>
	 * 
	 * <p>
	 * Output: String
	 * </p>
	 * 
	 */
	public static String TrichThang(String strDate) {
		//theo dinh dang:dd/MM/yyyy
////		if (strDate.indexOf("/") != -1) {
////			String arr[] = new String[3];
////
////			arr = strDate.split("/");
////
////			return (arr[1]);
//
//		}
		//theo dinh dang: yyyy-MM-dd
		 if (strDate.indexOf("-") != -1) {
			String arr[] = new String[3];

			arr = strDate.split("-");

			return (arr[1]);

		}
		//theo dinh dang:dd/MM/yyyy HH:SS
		else 
		{
			StringTokenizer token = new StringTokenizer(strDate,"/,: ");
			token.nextToken();
			return token.nextToken();
		}
		
	}

	/**
	 * <p>
	 * Description: Ham trich lay nam trong mot kieu date cho truoc
	 * </p>
	 * <p>
	 * Input: String strDate
	 * </p>
	 * 
	 * <p>
	 * Output: String
	 * </p>
	 * 
	 */
	public static String TrichNam(String strDate) {
		//theo dinh dang:dd/MM/yyyy
//		if (strDate.indexOf("/") != -1) {
//			String arr[] = new String[3];
//
//			arr = strDate.split("/");
//
//			return (arr[2]);
//		}
		//theo dinh dang: yyyy-MM-dd
		if (strDate.indexOf("-") != -1) {
			String arr[] = new String[3];

			arr = strDate.split("-");

			return (arr[0]);
		}
		//theo dinh dang:dd/MM/yyyy HH:SS
		else 
		{
			StringTokenizer token = new StringTokenizer(strDate,"/,: ");
			token.nextToken();
			token.nextToken();
			return token.nextToken();
		}
	}

	/**
	 * <p>
	 * Description: Ham trich lay ngay cuoi thang trong mot kieu date cho truoc
	 * </p>
	 * <p>
	 * Input: int Thang, int Nam
	 * </p>
	 * 
	 * <p>
	 * Output: Integer
	 * </p>
	 * 
	 */
	public static int LayNgayCuoiThang(int Thang, int Nam) {
		int lastDay;

		Calendar calendar = Calendar.getInstance();
		calendar.set(Nam, Thang - 1, 1);

		lastDay = calendar.getActualMaximum(Calendar.DATE);

		return (lastDay);
	}

	/**
	 * <p>
	 * Description: Ham Cong tru ngay thang
	 * </p>
	 * <p>
	 * Input: String strDate,int intNum
	 * </p>
	 * <p>
	 * Output: String
	 * </p>
	 * 
	 */
	public static String CongTruNgay(String strDate, int intNum) {
		String strResult = "";
		try {
			String DATE_FORMAT = "dd/MM/yyyy";
			int intNgay = Integer.parseInt(TrichNgay(strDate));
			int intThang = Integer.parseInt(TrichThang(strDate)) - 1;
			int intNam = Integer.parseInt(TrichNam(strDate));

			java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat(
					DATE_FORMAT);
			Calendar c1 = Calendar.getInstance();
			c1.set(intNam, intThang, intNgay);
			c1.add(Calendar.DAY_OF_MONTH, intNum); // substract 1 month
			strResult = sdf.format(c1.getTime());
		} catch (Exception e) {
			strResult = "";
		}
		return strResult;
	}

	
	/**************************************
	 * Method name		: CongTruThang
	 * Return type		: String
	 * Decription		:Cong tru thang
	 * Created date		: Jun 13, 2008
	 * Author			: Honglk
	 **************************************/
	public static String CongThang(String strDate, int intNum) {
		String strResult = "";
		try {
//			String DATE_FORMAT = "dd/MM/yyyy";
			String DATE_FORMAT = "yyyy-MM-dd";
			int intNgay = Integer.parseInt(TrichNgay(strDate));
			int intThang = Integer.parseInt(TrichThang(strDate)) - 1;
			int intNam = Integer.parseInt(TrichNam(strDate));

			SimpleDateFormat sdf = new SimpleDateFormat(
					DATE_FORMAT);
			Calendar c1 = Calendar.getInstance();
			c1.set(intNam, intThang, intNgay);
			c1.add(Calendar.MONTH, intNum); // substract 1 month
			strResult = sdf.format(c1.getTime());
		} catch (Exception e) {
			strResult = "";
		}
		return strResult;
	}
	
	/**************************************
	 * Method name		: truoc1Thang
	 * Return type		: String
	 * Decription		:lay truoc 1 thang,theo dinh dang nam-thang-ngay
	 * Created date		: Jun 14, 2008
	 * Author			: Honglk
	 **************************************/
	public static String truoc1Thang(String strDate) {
		String strResult = "";
		try {
			String DATE_FORMAT = "yyyy-MM-dd";
			int intNgay = Integer.parseInt(TrichNgay(strDate));
			int intThang = Integer.parseInt(TrichThang(strDate)) - 1;
			int intNam = Integer.parseInt(TrichNam(strDate));
			SimpleDateFormat sdf = new SimpleDateFormat(
					DATE_FORMAT);
			Calendar c1 = Calendar.getInstance();
			if(intThang == 1){
				intThang = 12;
				intNam = intNam - 1;
			}else{
				intThang = intThang - 1;
			}
			c1.set(intNam, intThang, intNgay);
			//c1.add(Calendar.MONTH, intNum); // substract 1 month
			strResult = sdf.format(c1.getTime());
		} catch (Exception e) {
			strResult = "";
		}
		return strResult;
	}
	
	
	static final long ONE_HOUR = 60 * 60 * 1000L;
	public static long daysBetween(Date d1, Date d2)
	{
	Long acc =  ( (d2.getTime() - d1.getTime() +
			ONE_HOUR) / (ONE_HOUR * 24));
	return acc;
	
	}
	
	public static String layNgayHetHan(String ngayve){
		String day = DateUtils.TrichNgay(ngayve);
		int intDay = Integer.parseInt(day);
		String month =DateUtils.TrichThang(ngayve);
		int intMonth = Integer.parseInt(month);
		String year = DateUtils.TrichNam(ngayve);
		//thang co 31 ngay
		if(intMonth == 1 || intMonth == 3 || intMonth == 5 || intMonth == 7 || intMonth == 8 || intMonth == 10 || intMonth == 12){
			if(intDay > 24){
				int acc = 31 - intDay;
				int temp = 7 - acc;
				intMonth+= 1;
				intDay= temp;
			}
			else  intDay+= 7;
		}
		else if(intMonth == 2){
			if(intDay > 21){
				int acc = 28 - intDay;
				int temp = 7 - acc;
				intMonth+= 1;
				intDay= temp;
			}
			else  intDay+= 7;
		}
		else {
			if(intDay > 23){
				int acc = 30 - intDay;
				int temp = 7 - acc;
				intMonth+= 1;
				intDay= temp;
			}
			else  intDay+= 7;
		}
		String strNgayHetHan = year+"-"+intMonth+"-"+intDay;
		System.out.println("ngay het han "+strNgayHetHan);
		return strNgayHetHan;
	}
	public static void main(String[] args) {
//		System.out.println(DateUtils.daysBetween(new java.util.Date(), DateUtils.sqlDateToutilDate(java.sql.Date.valueOf("2008-08-37"))));
	
	}
	
}
